<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
   public function login() {
		if (IS_POST) {
			$username = $_POST["username"];
			$password=md5($_POST["password"]);
			$m = M("member");
			$user = $m -> field("id,username,nickname") -> where("username='{$username}' and password='{$password}'") -> find();
			if ($user) {
				//查询用户权限
				session_start();
				$_SESSION["username"] = $username;
				$user["code"] = 0;
				$user["rules"] = $this -> GetRules($user["id"]);
				$this -> ajaxReturn($user);
			} else {
				$obj = (object) array();
				$obj -> code = 101;
				$obj -> message = "查询失败！";
				// 	 echo "用户名或密码错误";
				$this -> ajaxReturn($obj);
			}
		} else if (IS_GET) {
			$this -> display();
		}
	}
	//获取菜单项
	private function GetRules($uid) {
		$access = M("auth_group_access");
		$group = $access -> field("rules") -> join("think_auth_group on think_auth_group.id=think_auth_group_access.group_id") -> where("uid=" . $uid) -> find();
		$rule = M("auth_rule");
		$userRules = $rule -> where("id in (" . $group["rules"] . ") and is_menu=1") -> select();
		return $userRules;
	}
}